home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elib-006.lha / elib-0.06 / library / stack-m.el < prev    next >
Lisp/Scheme  |  1993-01-24  |  3KB  |  106 lines

  1. ;;;; $Id: stack-m.el,v 0.5 1992/08/19 01:58:02 ceder Exp $
  2. ;;;; This file implements a simple LIFO stack using macros.
  3. ;;;;
  4. ;;;; Copyright (C) 1991, 1992 Free Software Foundation
  5. ;;;;
  6. ;;;; This file is part of the GNU Emacs lisp library, Elib.
  7. ;;;;
  8. ;;;; GNU Elib is free software; you can redistribute it and/or modify
  9. ;;;; it under the terms of the GNU General Public License as published by
  10. ;;;; the Free Software Foundation; either version 1, or (at your option)
  11. ;;;; any later version.
  12. ;;;;
  13. ;;;; GNU Elib is distributed in the hope that it will be useful,
  14. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;;;; GNU General Public License for more details.
  17. ;;;;
  18. ;;;; You should have received a copy of the GNU General Public License
  19. ;;;; along with GNU Emacs; see the file COPYING.  If not, write to
  20. ;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. ;;;; 
  22. ;;;; Author: Inge Wallin
  23. ;;;; 
  24.  
  25. ;;;
  26. ;;; The stack is implemented as a linked list with a tag 'STACK
  27. ;;; as the first element.  All entries and removals are done using
  28. ;;; destructive functions.
  29. ;;;
  30. ;;; This file implements the functions as macros for speed in compiled 
  31. ;;; code.
  32. ;;;
  33.  
  34.  
  35. ;; Provide the function version and remove the macro version
  36. (provide 'stack-m)
  37. (setq features (delq 'stack-f features))
  38.  
  39.  
  40. ;;; ================================================================
  41.  
  42.  
  43. (defmacro stack-create ()
  44.   "Create an empty lifo stack."
  45.   (` (cons 'STACK nil)))
  46.  
  47.  
  48. (defmacro stack-p (stack)
  49.   "Return t if STACK is a stack, otherwise return nil."
  50.   (` (eq (car-safe (, stack)) 'STACK)))
  51.  
  52.  
  53. (defmacro stack-push (stack element)
  54.   "Push an element onto the stack.
  55. Args: STACK ELEMENT"
  56.   (` (setcdr (, stack) (cons (, element) (cdr (, stack))))))
  57.  
  58.  
  59. (defmacro stack-pop (stack)
  60.   "Remove the topmost element from STACK and return it. 
  61. If the stack is empty, return nil."
  62.   (` (prog1
  63.      (car-safe (cdr (, stack)))
  64.        (setcdr (, stack) (cdr-safe (cdr (, stack)))))))
  65.  
  66.  
  67. (defmacro stack-empty (stack)
  68.   "Return t if STACK is empty, otherwise return nil."
  69.   (` (null (cdr (, stack)))))
  70.  
  71.  
  72. (defmacro stack-top (stack)
  73.   "Return the topmost element of STACK or nil if it is empty."
  74.   (` (car-safe (cdr (, stack)))))
  75.  
  76.  
  77. (defmacro stack-nth (stack n)
  78.   "Return nth element of a stack, but don't remove it.
  79. Args: STACK N
  80. If the length of the stack is less than N, return nil.
  81.  
  82. The top stack element has number 0."
  83.   (` (nth (, n) (cdr (, stack)))))
  84.  
  85.  
  86. (defmacro stack-all (stack)
  87.   "Return a list of all entries in STACK.
  88. The element last pushed is first in the list."
  89.   (` (cdr (, stack))))
  90.  
  91.  
  92. (defmacro stack-copy (stack)
  93.   "Return a copy of STACK.
  94. All entries in STACK are also copied."
  95.   (` (cons 'STACK (copy-sequence (cdr (, stack))))))
  96.  
  97.  
  98. (defmacro stack-length (stack)
  99.   "Return the number of elements on STACK."
  100.   (` (length (cdr (, stack)))))
  101.  
  102.  
  103. (defmacro stack-clear (stack)
  104.   "Remove all elements from STACK."
  105.   (` (setcdr (, stack) nil)))
  106.